<template>
  <div class="container">
    <!-- <selectDataOrder
      :per-visible.sync="showOrder"
      @current="selectCurrentRow"
    /> -->

    <el-dialog
      append-to-body
      title="选择订单"
      :visible.sync="isVisible"
      :close-on-click-modal="false"
      custom-class="bodyNoPadDialog bigDialog"
      width="70%"
      @open="MarterdialogShow"
    >
      <div class="dialogDody marDialog flex flex_column">
        <div class="btnCon flex flex_between" style="border-bottom:none">
          <!--      搜索框-->
          <div class="search">
            <el-input
              v-model="materalSearch.likeWords"
              prefix-icon="el-icon-search"
              placeholder="输入关键字"
              size="small"
              @keyup.enter.native="MarterdialogShow"
            >
            </el-input>
          </div>
        </div>
        <!-- 表格区域       -->
        <div class="tableCon not pt0" style="padding: 16px">
          <el-table :data="materialList" v-loading="loading" border style="width: 100%" height="500">
            <el-table-column prop="xieYiShuHao" label="订单单号" width="200" />
            <el-table-column prop="orderTypeName" label="订单类型" width="100" />
            <el-table-column prop="orderCreateTime" label="创建时间" width="150"/>
            <el-table-column prop="orderProductName" label="产品名称" width="200" show-overflow-tooltip />
            <el-table-column prop="printingSurface" label="版面" />
            <el-table-column prop="productVersion" label="版本" />
            <el-table-column prop="taoZhuangQuantity" label="套数" />
            <el-table-column prop="expectedDeliveryTime" label="交货日期" width="150" />
            <el-table-column width="105" prop="surplusDeliverGoodsDays" label="剩余日期" />
            <el-table-column width="135" prop="customerName" label="所属客户" show-overflow-tooltip />
            <el-table-column width="105" prop="salesmanName" label="业务人员" />
            <el-table-column
              label="操作"
              width="100px"
              fixed="right"
              align="center"
            >
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="medium"
                  style="color: #606266"
                  @click="selectCurrentRow(scope.row)"
                >
                  选择
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 S-->
      <div class="d_pag">
        <pagination
          :total="orderTotal || 0"
          :page.sync="materalSearch.pageIndex"
          :limit.sync="materalSearch.pageSize"
          @pagination="MarterdialogShow"
        />
      </div>
        </div>
      </div>
    </el-dialog>

    <div class="search-box">
      <el-form :model="search" label-width="80px">
        <el-row :gutter="24">
          <el-col :span="6">
            <div class="flex">
              <el-form-item label="订单单号"> </el-form-item>
              <el-input v-model="search.likeWords" size="small" placeholder="请输入或选择订单号">
                <el-button
                  slot="append"
                  type="success"
                  size="small"
                  @click="isVisible = true"
                  >选择</el-button
                >
              </el-input>
            </div>
          </el-col>
          <el-col :span="11">
            <div class="flex">
              <el-form-item label="日期范围">
                <el-date-picker
                  v-model="valueDate"
                  style="width: 100%"
                  size="small"
                  type="daterange"
                  range-separator="-"
                  value-format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  @change="handleDateChange"
                >
                </el-date-picker>
              </el-form-item>
              <div>
                <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-search"
                  style="margin-left: 20px"
                  :loading="searchLoading"
                  @click="getAllMaterils"
                  >搜索</el-button
                >
                <el-button
                  size="small"
                  icon="el-icon-download"
                  :loading="downLoading"
                  @click="downloadFile"
                  >导出数据</el-button
                >
              </div>
            </div>
          </el-col>
          <!-- <el-col :span="6"> </el-col> -->
        </el-row>
      </el-form>
    </div>
    <div class="d_main flex1">
      <!-- 操作区 S-->
      <!-- 操作区 E-->
      <!-- 表格操作区 S-->
      <div class="d_tableOperation">
        <div class="d_condition"></div>
        <div class="d_set">
          <div @click="Refresh"><i class="d_icon el-icon-refresh-right" /></div>
          <div class="setting flex">
            <HeadSeting
              :list-data.sync="tableHead"
              :table-code="$config.havegxexceptionreportlist"
              @reset="getTableHead"
            />
          </div>
        </div>
      </div>
      <!-- 表格操作区 E-->
      <!--table板块-->
      <div ref="tableCon" class="tableCon expandBox">
        <el-table
          v-show="tableHead.length > 0"
          id="emtable"
          ref="multipleTable"
          v-loading="searchLoading"
          :data="allMaterirls"
          :height="tableHeight || 'auto'"
          border
          fit
          style="width: 100%"
          @header-dragend="surverWidth"
        >
          <template v-for="(fruit, index) in tableHead">
            <el-table-column
              v-if="fruit.visible"
              :key="index"
              :label="fruit.columnName"
              :min-width="fruit.columnWidth || 90"
              :prop="fruit.columnCode"
              show-overflow-tooltip
              :fixed="fruit.isFloat ? 'left' : false"
            >
              <template slot-scope="scope">
                <template>
                  {{ scope.row[fruit.columnCode] }}
                </template>
              </template></el-table-column
            >
          </template>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
            <span @click="handleSee(scope.row)" style="cursor: pointer;">查看</span>
            </template>
          </el-table-column>
        </el-table>
        <!-- 骨架屏     -->
        <div v-if="tableHead.length == 0" class="p16">
          <el-skeleton :rows="6" animated />
        </div>
      </div>
      <!-- 分页 S-->
      <div class="d_pag">
        <pagination
          :total="total || 0"
          :page.sync="search.pageIndex"
          :limit.sync="search.pageSize"
          @pagination="getAllMaterils"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { arraySort } from "@/utils";
import HeadSeting from "@/components/HeadSeting";
import Pagination from "@/components/Pagination";
import { mapGetters } from "vuex";
import { fileExport } from "@/utils/index";
import selectDataOrder from "@/components/selectDataOrder";
import router from '@/router';
export default {
  name: "abnormalQueryList",
  components: {
    Pagination,
    HeadSeting,
    selectDataOrder,
  },
  data() {
    return {
      inStockTypeList: [], //物料入库类型下拉数据
      
      materialList:[],  //订单数据
      searchLoading: false, //表格加载层
      downLoading: false, //下载文件按钮加载层
      showOrder: false,
      valueDate: "",
      isVisible:false,  //选择订单弹出层状态
      allMaterirls: [],
      tableHeight: null,
      // 搜索条件
      search: {
        pageIndex: 1,
        pageSize: 20,
        likeWords: "",
        beginDate: "",
        endDate: "",
      },
      loading:false,

      materalSearch:{
        pageIndex: 1,
        pageSize: 20,
        likeWords: "",
        QueryDataBaseFiled:''
      },
      orderTotal:0,
      total: 0, // 总条数
      tableHead: [], // 表格表头
    };
  },
  computed: {
    ...mapGetters(["name", "userInfo"]),
  },
  created() {
    //  所有列表数据
    this.getAllMaterils();
    // 表格得表头
    this.getTableHead();
  },
  mounted() {
    // 表格高度
    this.calHeight();
  },
  updated() {
    this.$nextTick(() => {
      this.$refs.multipleTable.doLayout();
    });
  },
  methods: {

      //获取订单数据
      MarterdialogShow(){
        this.loading = true
        this.$api.report.GetPageValidSaleOrderDataList(this.materalSearch).then((res)=>{
          this.materialList = res.list
          this.orderTotal = res.totalCount;
          this.loading = false
        }).catch(()=>{
          this.loading = false
        })
      },

    //选择订单事件
    selectCurrentRow(row) {
      this.search.likeWords = row.xieYiShuHao;
      this.isVisible = false
      this.getAllMaterils();
    },

    //日期范围选择事件
    handleDateChange() {
      if (this.valueDate) {
        this.search.beginDate = this.valueDate[0];
        this.search.endDate = this.valueDate[1];
      } else {
        this.search.beginDate = "";
        this.search.endDate = "";
      }
    },

    //导出数据
    downloadFile() {
      this.downLoading = true;
      this.$api.report
        .ExportHaveCraftExceptionsOrdersReportsToExcel(this.search)
        .then((res) => {
          fileExport(res, "异常工艺导出数据", "xlsx");
          this.downLoading = false;
        });
    },

    //查看详情
    handleSee(row){
        this.$router.push({
        path: '/report/abnormalQueryDetail', // 跳转的路径
        query: {
          orderNo: row.orderNo,
        }
      })
    },

    // 保存表头样式变化
    saveHead(newArray) {
      const parmas = newArray;
      setTimeout(() => {
        this.$api.common.saveTableStyle(parmas).then(() => {});
      }, 300);
    },
    // 保存表头列宽度
    surverWidth(newWidth, oldWidth, column) {
      setTimeout(() => {
        const heads = [...this.tableHead];
        heads.forEach((item) => {
          if (item.columnCode === column.property) {
            item.columnWidth = column.width;
            return;
          }
        });
        this.saveHead(heads);
      }, 100);
    },
    // 表格最大高度
    calHeight() {
      this.$nextTick(() => {
        const rect = this.$refs.tableCon.getBoundingClientRect();
        console.log(rect.height);
        this.tableHeight = rect.height;
      });
    },
    // ，清除所有查询条件
    Refresh() {
      this.search = this.$options.data().search;
      this.getAllMaterils();
    },
    // 获取表格表头
    getTableHead() {
      const parmas = {
        tableCode: this.$config.havegxexceptionreportlist,
      };
      this.$api.common.getAllTableHead(parmas).then((res) => {
        console.log(res);
        res.sort(arraySort("columnSort"));
        this.tableHead = res;
      });
    },
    // 所有列表数据
    getAllMaterils() {
      const parmas = this.search;
      this.searchLoading = true;
      this.$api.report
        .PageQueryHaveCraftExceptionsOrdersReportsData(parmas)
        .then((res) => {
          this.allMaterirls = res.list;
          this.total = res.totalCount;
          this.searchLoading = false;
        })
        .catch(() => {
          this.searchLoading = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.search-box {
  padding: 14px 16px 0 0;
  margin-bottom: 10px;
  height: 70px;
  background: #ffffff;
  border-radius: 4px;
  border: 1px solid #e6e6e6;
  .el-form-item {
    margin-bottom: 0px !important;
  }
}
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.permangeRg {
  border: 1px solid #e6e6e6;
  background: #fff;
  height: 100%;

  .mainTT {
    font-size: 16px;
    font-weight: bold;
    width: 100%;
    padding: 18px 16px;
    padding-bottom: 0;
  }
}
.addWuliaoBox {
  padding: 10px 16px;

  .tableBox {
    margin-top: 16px;
    border: 1px solid #e6e6e6;
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    overflow: hidden;
  
    .tt {
      height: 38px;
      line-height: 38px;
      padding-left: 16px;
      background: #f7f8fa;
      border-radius: 8px 8px 0 0;
      
    }
  }
}

.subTip {
  border-bottom: 1px #e6e6e6 solid;
  background: #f5f6f9;
  padding: 8px 16px;
}
</style>
